//Standard
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

//Added
using System.IO;

namespace Mean.Net
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            decimal blah = 0;
            string theNumber;

            if(txtNumToAdd.Text.Length < 1)
            {
                txtNumToAdd.Text = "0";

            }

            if (txtDecimal.Text.Length < 1)
            {
                txtDecimal.Text = "0";

            }
            theNumber = txtNumToAdd.Text + "." + txtDecimal.Text;
            lstGrades.Items.Add(theNumber);

            for (int i = 0; i < lstGrades.Items.Count; i++)
            {
                lstGrades.SetSelected(i, true);
                blah = blah + Convert.ToDecimal(lstGrades.Text);

            }
            txtCurrent.Text = Convert.ToString(blah / lstGrades.Items.Count);
            txtNumToAdd.Clear();
            txtDecimal.Clear();
            txtNumToAdd.Focus();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            lstGrades.Items.Clear();
            txtDecimal.Clear();
            txtCurrent.Clear();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            decimal percentage;
            string percentageStr;

            //Make sure there's some data in the text boxes.
            if (Convert.ToDecimal(txtCorrect.Text.Length) < 1)
            {
                txtCorrect.Text = "1";
            }

            if (Convert.ToDecimal(txtTotal.Text.Length) < 1)
            {
                txtTotal.Text = "1";
            }

            //x / y = w.ff
            //x out of y; w would be a whole number, ff is a fraction. 1 is 100%,
            //.99 is 99% and so on
            percentage = Convert.ToDecimal(Convert.ToDecimal(txtCorrect.Text) / Convert.ToDecimal(Convert.ToDecimal(txtTotal.Text)));
            //Turn the result in to a string so we can remove the decimal
            percentageStr = Convert.ToString(percentage);
            percentageStr.Replace(".", "");


            if (Convert.ToDecimal(txtCorrect.Text.Length) > Convert.ToDecimal(txtTotal.Text.Length))
            {
                percentageStr.Replace("0.", "");
            }

            //Display the percentage and clear the input fields
            txtPercent.Text = percentageStr + "%";
            txtCorrect.Clear();
            txtTotal.Clear();
        }

        private void btnCSV_Click(object sender, EventArgs e)
        {

            lstGrades.Items.Clear();

            string file_name = "";
            string file_data = "";

            string[] file_data_parsed;

            dlgOpen.ShowDialog();
            file_name = dlgOpen.FileName;

            if (dlgOpen.FileName.Length > 0 && dlgOpen.CheckFileExists == true)
            {
              
                decimal running_total = 0;
                StreamReader value_list = new StreamReader(file_name);
                
                file_data = value_list.ReadToEnd();

                file_data_parsed = file_data.Split();
                for (int i = 0; i < file_data_parsed.GetLength(0); i++)
                {
                    lstGrades.Items.Add(file_data_parsed[i]);
                }

                for (int i = 0; i < lstGrades.Items.Count; i++)
                {
                    

                    running_total = running_total + Convert.ToDecimal(file_data_parsed[i]);
                }
                txtCurrent.Text = Convert.ToString(running_total / lstGrades.Items.Count);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }




    }
}